//
//  AppointmentTableView.m
//  HarmonizePub
//
//  Created by rzhy on 16/9/13.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "AppointmentTableView.h"
#import "Config.h"

@interface AppointmentTableView () <UITableViewDataSource, UITableViewDelegate>
{
    NSInteger _selectRow;
}

@end

@implementation AppointmentTableView

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.dataSource = self;
    self.delegate = self;
    self.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    [self reloadData];
    if (self.isLeftTable && dataArray.count) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
        [self tableView:self didSelectRowAtIndexPath:indexPath];
    }
}

#pragma mark - tableView delegate/dataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        if (self.isLeftTable) {
            cell.selectedBackgroundView = [self createCellSelectedBgView];
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
        } else {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.textColor = RGBColor(39, 155, 219);
            cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
            cell.textLabel.numberOfLines = 0;
        }
    }
    NSString *string;
    if (self.isLeftTable) {
        string = self.dataArray[indexPath.row];
        if (indexPath.row == _selectRow) cell.textLabel.textColor = RGBColor(39, 155, 219);
        else cell.textLabel.textColor = [UIColor blackColor];
    } else {
        string = self.dataArray[indexPath.row][@"ksmc"];
    }
    cell.textLabel.text = string;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isLeftTable) {
        _selectRow = indexPath.row;
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.textLabel.textColor = RGBColor(39, 155, 219);
    } else {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    if ([self.tableDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
        [self.tableDelegate tableView:self didSelectRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isLeftTable) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.textLabel.textColor = [UIColor blackColor];
    }
}

#pragma mark - selectView
- (UIImageView *)createCellSelectedBgView {
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.backgroundColor = [UIColor whiteColor];
    return imageView;
}

@end
